package com.ruisi.thumbnail;

import java.io.File;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;

/**
 * BitmapDecodeRunable负责将图片从磁盘加载到内存
 * 
 * @author spring ming
 *
 */
public class BitmapDecodeRunnable implements Runnable{
	
	public static final int IMAGE_LOAD_SUCCESS = 101;
	public static final int IMAGE_LOAD_FAIL    = 102;
	
	private DecodeTask mTask;
	private Handler mCallbackHandler;
	/**
	 * 构造函数
	 * 
	 * @param localPath          本地路径
	 * @param listener           加载图片的监听者
	 * @param callbackHandlers   回调用的handler
	 */
	BitmapDecodeRunnable(DecodeTask task,
					     Handler callbackHandler){
		super();
		
		mTask            = task;
		mCallbackHandler = callbackHandler;
	}
	
	@Override
	public void run() {
		File file = new File(mTask.mLocalPath);
		if(!file.exists()){
			mCallbackHandler.obtainMessage(IMAGE_LOAD_FAIL, 0, 0, mTask).sendToTarget();
			return;
		}
		
		Bitmap bmp = BitmapFactory.decodeFile(mTask.mLocalPath);
		if(null == bmp){
			mCallbackHandler.obtainMessage(IMAGE_LOAD_FAIL, 0, 0, mTask).sendToTarget();
		}else{
			mTask.mBitmap = bmp;
			mCallbackHandler.obtainMessage(IMAGE_LOAD_SUCCESS, 0, 0, mTask).sendToTarget();
		}
		
	}
}
